home *** CD-ROM | disk | FTP | other *** search
/ Clickx 63 / Clickx 63.iso / software / multimedia / mirov204 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / commonDialog.js < prev    next >
Encoding:
JavaScript  |  2008-01-29  |  10.9 KB  |  332 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Alec Flett  <alecf@netscape.com>
  23.  *   Ben Goodger <ben@netscape.com>
  24.  *   Blake Ross  <blakeross@telocity.com>
  25.  *   Joe Hewitt <hewitt@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
  42. var gCommonDialogParam = 
  43.   window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  44.   
  45. function showControls()
  46. {
  47.   // This is called before onload fires, so we can't be certain that any elements
  48.   // in the document have their bindings ready, so don't call any methods/properties
  49.   // here on xul elements that come from xbl bindings.
  50.   
  51.   // show the required textboxes and set their initial values
  52.   var nTextBoxes = gCommonDialogParam.GetInt(3);
  53.   if (nTextBoxes == 2) {
  54.     if (gCommonDialogParam.GetInt(4) == 1) {
  55.       initTextbox("password1", 4, 6, false);
  56.       initTextbox("password2", 5, 7, false);
  57.     }
  58.     else {
  59.       initTextbox("login", 4, 6, false);
  60.       initTextbox("password1", 5, 7, false);
  61.     }
  62.   } else if (nTextBoxes == 1) {
  63.     if (gCommonDialogParam.GetInt(4) == 1)
  64.       initTextbox("password1", -1, 6, true);
  65.     else
  66.       initTextbox("login", 4, 6, true);
  67.   }
  68. }
  69.  
  70. function setLabelForNode(aNode, aLabel, aIsLabelFlag)
  71. {
  72.   // This is for labels which may contain embedded access keys.
  73.   // If we end in (&X) where X represents the access key, optionally preceded
  74.   // by spaces and/or followed by the ':' character, store the access key and
  75.   // remove the access key placeholder + leading spaces from the label.
  76.   // Otherwise a character preceded by one but not two &s is the access key.
  77.   // Store it and remove the &.
  78.  
  79.   // Note that if you change the following code, see the comment of
  80.   // nsTextBoxFrame::UpdateAccessTitle.
  81.   var accessKey = null;
  82.   if (/ *\(\&([^&])\)(:)?$/.test(aLabel)) {
  83.     aLabel = RegExp.leftContext + RegExp.$2;
  84.     accessKey = RegExp.$1;
  85.   } else if (/^(.*[^&])?\&(([^&]).*$)/.test(aLabel)) {
  86.     aLabel = RegExp.$1 + RegExp.$2;
  87.     accessKey = RegExp.$3;
  88.   }
  89.  
  90.   // && is the magic sequence to embed an & in your label.
  91.   aLabel = aLabel.replace(/\&\&/g, "&");
  92.   if (aIsLabelFlag) {    // Set text for <label> element
  93.     aNode.setAttribute("value", aLabel);
  94.   } else {    // Set text for other xul elements
  95.     aNode.label = aLabel;
  96.   }
  97.  
  98.   // XXXjag bug 325251
  99.   // Need to set this after aNode.setAttribute("value", aLabel);
  100.   if (accessKey)
  101.     aNode.accessKey = accessKey;
  102. }
  103.  
  104. function commonDialogOnLoad()
  105. {
  106.   // set the document title
  107. //@line 110 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/content/commonDialog.js"
  108.   document.title = gCommonDialogParam.GetString(12);
  109. //@line 112 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/content/commonDialog.js"
  110.  
  111.   // set the number of command buttons
  112.   var nButtons = gCommonDialogParam.GetInt(2);
  113.   var dialog = document.documentElement;
  114.   switch (nButtons) {
  115.     case 1:
  116.       dialog.getButton("cancel").hidden = true;
  117.       break;
  118.     case 4:
  119.       dialog.getButton("extra2").hidden = false;
  120.     case 3:
  121.       dialog.getButton("extra1").hidden = false;
  122.   }
  123.  
  124.   // display the main text
  125.   // XXX the substr(0, 10000) part is a workaround for bug 317334
  126.   var croppedMessage = gCommonDialogParam.GetString(0).substr(0, 10000);
  127.   setElementText("info.body", croppedMessage, true);
  128.  
  129.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  130.  
  131.   // set the icon
  132.   var iconElement = document.getElementById("info.icon");
  133.   var iconClass = gCommonDialogParam.GetString(2);
  134.   if (!iconClass)
  135.     iconClass = "message-icon";
  136.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  137.  
  138.   switch (nButtons) {
  139.     case 4:
  140.       setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
  141.       // fall through
  142.     case 3:
  143.       setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
  144.       // fall through
  145.     default:
  146.     case 2:
  147.       var string = gCommonDialogParam.GetString(9);
  148.       if (string)
  149.         setLabelForNode(document.documentElement.getButton("cancel"), string);
  150.       // fall through
  151.     case 1:
  152.       string = gCommonDialogParam.GetString(8);
  153.       if (string)
  154.         setLabelForNode(document.documentElement.getButton("accept"), string);
  155.       break;
  156.   }
  157.  
  158.   // set default result to cancelled
  159.   gCommonDialogParam.SetInt(0, 1); 
  160.  
  161.   // initialize the checkbox
  162.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  163.  
  164.   if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
  165.   {
  166.     var dlgButtons = ['accept', 'cancel', 'extra1', 'extra2'];
  167.  
  168.     // Set the default button and focus it on non-OS X systems
  169.     var dButton = dlgButtons[gCommonDialogParam.GetInt(5)];
  170.     document.documentElement.defaultButton = dButton;
  171. //@line 174 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/content/commonDialog.js"
  172.     document.documentElement.getButton(dButton).focus();
  173. //@line 176 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/content/commonDialog.js"
  174.   }
  175.  
  176.   if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
  177.   {
  178.     var delayInterval = 2000;
  179.     try {
  180.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  181.                   .getService(Components.interfaces.nsIPrefBranch);
  182.       delayInterval = prefs.getIntPref("security.dialog_enable_delay");
  183.     } catch (e) {}
  184.  
  185.     document.documentElement.getButton("accept").disabled = true;
  186.     document.documentElement.getButton("extra1").disabled = true;
  187.     document.documentElement.getButton("extra2").disabled = true;
  188.  
  189.     setTimeout(commonDialogReenableButtons, delayInterval);
  190.     
  191.     addEventListener("blur", commonDialogBlur, false);
  192.     addEventListener("focus", commonDialogFocus, false);
  193.   }
  194.  
  195.   getAttention();
  196. }
  197.  
  198. var gDelayExpired = false;
  199. var gBlurred = false;
  200.  
  201. function commonDialogBlur(aEvent)
  202. {
  203.   if (aEvent.target != document)
  204.     return;
  205.   gBlurred = true;
  206.   document.documentElement.getButton("accept").disabled = true;
  207.   document.documentElement.getButton("extra1").disabled = true;
  208.   document.documentElement.getButton("extra2").disabled = true;
  209. }
  210.  
  211. function commonDialogFocus(aEvent)
  212. {
  213.   if (aEvent.target != document)
  214.     return;
  215.   gBlurred = false;
  216.   // When refocusing the window, don't enable the buttons unless the countdown
  217.   // delay has expired. 
  218.   if (gDelayExpired) {
  219.     var script = "document.documentElement.getButton('accept').disabled = false; ";
  220.     script += "document.documentElement.getButton('extra1').disabled = false; ";
  221.     script += "document.documentElement.getButton('extra2').disabled = false;";
  222.     setTimeout(script, 250);
  223.   }
  224. }
  225.  
  226. function commonDialogReenableButtons()
  227. {
  228.   // Don't automatically enable the buttons if we're not in the foreground
  229.   if (!gBlurred) {
  230.     document.documentElement.getButton("accept").disabled = false;
  231.     document.documentElement.getButton("extra1").disabled = false;
  232.     document.documentElement.getButton("extra2").disabled = false;
  233.   }
  234.   gDelayExpired = true;
  235. }
  236.  
  237. function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
  238. {
  239.   unHideElementById(aName+"Container");
  240.  
  241.   var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
  242.   if (label || aAlwaysLabel && !label)
  243.     setElementText(aName+"Label", label);
  244.     
  245.   var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
  246.   var textbox = document.getElementById(aName + "Textbox");
  247.   textbox.setAttribute("value", value);
  248. }
  249.  
  250. function setElementText(aElementID, aValue, aChildNodeFlag)
  251. {
  252.   var element = document.getElementById(aElementID);
  253.   if (!aChildNodeFlag && element) {
  254.     setLabelForNode(element, aValue, true);
  255.   } else if (aChildNodeFlag && element) {
  256.     element.appendChild(document.createTextNode(aValue));
  257.   }
  258. }
  259.  
  260. function setCheckbox(aChkMsg, aChkValue)
  261. {
  262.   if (aChkMsg) {
  263.     unHideElementById("checkboxContainer");
  264.     
  265.     var checkboxElement = document.getElementById("checkbox");
  266.     setLabelForNode(checkboxElement, aChkMsg);
  267.     checkboxElement.checked = aChkValue > 0;
  268.   }
  269. }
  270.  
  271. function unHideElementById(aElementID)
  272. {
  273.   var element = document.getElementById(aElementID);
  274.   element.hidden = false;
  275. }
  276.  
  277. function hideElementById(aElementID)
  278. {
  279.   var element = document.getElementById(aElementID)
  280.   element.hidden = true;
  281. }
  282.  
  283. function isVisible(aElementId)
  284. {
  285.   return document.getElementById(aElementId).hasAttribute("hidden");
  286. }
  287.  
  288. function onCheckboxClick(aCheckboxElement)
  289. {
  290.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  291. }
  292.  
  293. function commonDialogOnAccept()
  294. {
  295.   gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
  296.  
  297.   var numTextBoxes = gCommonDialogParam.GetInt(3);
  298.   var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
  299.   
  300.   if (numTextBoxes >= 1) {
  301.     var editField1;
  302.     if (textboxIsPassword1)
  303.       editField1 = document.getElementById("password1Textbox");
  304.     else
  305.       editField1 = document.getElementById("loginTextbox");
  306.     gCommonDialogParam.SetString(6, editField1.value);
  307.   }
  308.  
  309.   if (numTextBoxes == 2) {
  310.     var editField2;
  311.     if (textboxIsPassword1)
  312.       // we had two password fields
  313.       editField2 = document.getElementById("password2Textbox");
  314.     else
  315.       // we only had one password field (and one login field)
  316.       editField2 = document.getElementById("password1Textbox");
  317.     gCommonDialogParam.SetString(7, editField2.value);
  318.   }
  319. }
  320.  
  321. function commonDialogOnExtra1()
  322. {
  323.   gCommonDialogParam.SetInt(0, 2);
  324.   window.close();
  325. }
  326.  
  327. function commonDialogOnExtra2()
  328. {
  329.   gCommonDialogParam.SetInt(0, 3);
  330.   window.close();
  331. }
  332.